namespace Microsoft.VisualStudio.EnterpriseTools.OperationDesigner
{
    using Microsoft.VisualStudio.Modeling;
    using System;
    using Microsoft.VisualStudio.Modeling.Shell;
    using EastBancTech.InstallerDefinition;

    public class NameColumn : ColumnDescriptor
    {
        public NameColumn() : base("Name")
        {
        }

        //public override void AddEventHandlers(Store store)
        //{
        //    store.EventManagerDirectory.ElementAttributeChanged.Add(store.MetaDataDirectory.FindMetaAttribute(NamedElement.NameMetaAttributeGuid), new ElementAttributeChangedEventHandler(this.OnNameChanged));
        //}

        public override object GetValue(object component)
        {
            ModelElement element = component as ModelElement;
            if (element == null)
            {
                return string.Empty;
            }
            NamedElement member = element as NamedElement;
            if (member != null)
            {
                return member.Name;
            }

            return "No value";
        }

        public override OperationDesignerValueSupportedStates GetValueSupported(object component)
        {
            //ClrMethod method = component as ClrMethod;
            //if ((method != null) && method.IsNameReadOnly)
            //{
            //    return OperationDesignerValueSupportedStates.Unsupported;
            //}
            return base.GetValueSupported(component);
        }

        //private void OnNameChanged(object sender, ElementAttributeChangedEventArgs e)
        //{
        //    if (base.Host != null)
        //    {
        //        base.Host.Refresh(e.ModelElement);
        //    }
        //}

        //public override void RemoveEventHandlers(Store store)
        //{
        //    store.EventManagerDirectory.ElementAttributeChanged.Remove(store.MetaDataDirectory.FindMetaAttribute(NamedElement.NameMetaAttributeGuid), new ElementAttributeChangedEventHandler(this.OnNameChanged));
        //}

        public sealed override void SetValue(object component, object value)
        {
            Transaction transaction2;
            IDisposable disposable;
            ModelElement element = component as ModelElement;
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            ModelingDocData docData = null;
            IMonitorSelectionService service = (IMonitorSelectionService) element.Store.GetService(typeof(IMonitorSelectionService));
            if (service != null)
            {
                docData = service.CurrentDocument as ModelingDocData;
            }
            //ClrElement element2 = element as ClrElement;
            //bool doesArtifactExist = (element2 != null) && element2.DoesArtifactExist;
            //LinkedTransaction transaction = null;
            //if (doesArtifactExist)
            //{
            //    transaction = new LinkedTransaction(this.SetValueTransactionName, docData, element.Store);
            //    transaction2 = transaction.Transaction;
            //    disposable = transaction;
            //}
            //else
            //{
            //    transaction2 = element.Store.TransactionManager.BeginTransaction(this.SetValueTransactionName);
            //    disposable = transaction2;
            //}
            //using (IDisposable disposable2 = disposable)
            //{
            //    this.SetValueCore(element, value, doesArtifactExist);
            //    if (doesArtifactExist)
            //    {
            //        transaction.Commit();
            //    }
            //    else
            //    {
            //        transaction2.Commit();
            //    }
            //}
        }

        protected virtual void SetValueCore(ModelElement element, object value, bool doesArtifactExist)
        {
        //    ClrElement element2 = element as ClrElement;
        //    if ((element2 != null) && doesArtifactExist)
        //    {
        //        try
        //        {
        //            if (!element2.RenameRefactor(value as string, false))
        //            {
        //                goto Label_0023;
        //            }
        //        }
        //        catch (InvalidOperationException)
        //        {
        //        }
        //        return;
        //    }
        //Label_0023:
        //    element.SetAttributeValue(NamedElement.NameMetaAttributeGuid, value);
        }

        public override string Category
        {
            get
            {
                return "Category";
            }
        }

        protected virtual string SetValueTransactionName
        {
            get
            {
                return "SetValueTransaction";
            }
        }
    }
}

